package ar.edu.itba.it.paw.web;

import org.apache.wicket.IConverterLocator;
import org.apache.wicket.Page;
import org.apache.wicket.Request;
import org.apache.wicket.RequestCycle;
import org.apache.wicket.ResourceReference;
import org.apache.wicket.Response;
import org.apache.wicket.Session;
import org.apache.wicket.protocol.http.WebApplication;
import org.apache.wicket.protocol.http.WebRequest;
import org.apache.wicket.spring.injection.annot.SpringComponentInjector;
import org.apache.wicket.util.convert.ConverterLocator;
import org.apache.wicket.util.file.File;
import org.hibernate.SessionFactory;
import org.postgresql.core.Field;
import org.springframework.beans.factory.annotation.Autowired;

import ar.edu.itba.it.paw.domain.Category;
import ar.edu.itba.it.paw.domain.Product;
import ar.edu.itba.it.paw.domain.Question;
import ar.edu.itba.it.paw.domain.User;
import ar.edu.itba.it.paw.repo.UserRepo;
import ar.edu.itba.it.paw.web.common.HibernateWebRequestCycle;
import ar.edu.itba.it.paw.web.converter.CategoryConverter;
import ar.edu.itba.it.paw.web.converter.ProductConverter;
import ar.edu.itba.it.paw.web.converter.QuestionConverter;
import ar.edu.itba.it.paw.web.converter.UserConverter;



public class MercadoLibre extends WebApplication {

	public static final ResourceReference ADD_ICON = new ResourceReference(MercadoLibre.class, "resources/add.png");
	public static final ResourceReference EDIT_ICON = new ResourceReference(MercadoLibre.class, "resources/edit.gif");
	public static final ResourceReference DELETE_ICON = new ResourceReference(MercadoLibre.class, "resources/delete.gif");
	public static final ResourceReference MAIN_ICON = new ResourceReference(MercadoLibre.class, "resources/image.png");
	public static final ResourceReference DETAIL_ICON = new ResourceReference(MercadoLibre.class,"resources/magnifGlass.jpeg");
	private final SessionFactory sessionFactory;
	
	private UserRepo userRepo;
	
	@Autowired
	public MercadoLibre(SessionFactory sessionFactory,UserRepo userRepo) {
		this.sessionFactory = sessionFactory;
		this.userRepo = userRepo;
	}
	
	@Override
	public Class<? extends Page> getHomePage() {
		return HomePage.class;
	}

	@Override
	protected void init() {
		super.init();
		addComponentInstantiationListener(new SpringComponentInjector(this));
		getApplicationSettings().setPageExpiredErrorPage(HomePage.class);
		getApplicationSettings().setInternalErrorPage(ErrorPage.class);
	}

	@Override
	public Session newSession(Request request, Response response) {
		return new MercadoLibreSession(request);
	}

	@Override
	protected IConverterLocator newConverterLocator() {
		ConverterLocator converterLocator = new ConverterLocator();
		converterLocator.set(Category.class, new CategoryConverter());
		converterLocator.set(Product.class, new ProductConverter());
		converterLocator.set(Question.class, new QuestionConverter());
		converterLocator.set(User.class, new UserConverter(userRepo));
		return converterLocator;
	}
	
	@Override
	public RequestCycle newRequestCycle(Request request, Response response) {
		return new HibernateWebRequestCycle(this, (WebRequest) request, response, sessionFactory);
	}
}
